import 'package:easy_chat_app/commons/utils/RouteUtils.dart';
import 'package:easy_chat_app/commons/utils/StringUtils.dart';
import 'package:easy_chat_app/commons/utils/TokenUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';

class MobileRegisterPage extends StatefulWidget {
  const MobileRegisterPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _MobileRegisterPage();
  }
}

class _MobileRegisterPage extends State<MobileRegisterPage> {
  Logger logger = Logger();

  FocusNode accountFocusNode = FocusNode();
  FocusNode passwordFocusNode = FocusNode();
  FocusNode verifyPasswordFocusNode = FocusNode();

  TextEditingController accountTextEditingController = TextEditingController();
  TextEditingController passwordTextEditingController = TextEditingController();
  TextEditingController verifyPasswordTextEditingController =
      TextEditingController();

  String warningMsg = "";

  bool accountErrStatus = false;
  bool passwordErrStatus = false;
  bool verifyPasswordErrStatus = false;

  @override
  void dispose() {
    super.dispose();
    accountTextEditingController.dispose();
    passwordTextEditingController.dispose();
  }

  void setWarningMsg(String msg, bool accountErrStatus, bool passwordErrStatus,
      bool verifyPasswordErrStatus) {
    warningMsg = msg;
    this.accountErrStatus = accountErrStatus;
    this.passwordErrStatus = passwordErrStatus;
    this.verifyPasswordErrStatus = verifyPasswordErrStatus;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    accountFocusNode.addListener(() {
      if (accountFocusNode.hasFocus) {
        accountErrStatus = false;
        setState(() {});
      }
    });

    passwordFocusNode.addListener(() {
      if (passwordFocusNode.hasFocus) {
        passwordErrStatus = false;
        setState(() {});
      }
    });

    return Scaffold(
      appBar: AppBar(
        title: const Text("账号注册"),
        centerTitle: true,
      ),
      body: SafeArea(
        child: Container(
          height: double.infinity,
          color: Colors.white,
          child: SingleChildScrollView(
            padding: const EdgeInsets.only(top: 100, left: 20, right: 20),
            child: Column(
              children: [
                const FlutterLogo(
                  size: 100,
                ),
                const SizedBox(
                  width: double.infinity,
                  height: 20,
                ),
                CupertinoApp(
                  debugShowCheckedModeBanner: false,
                  home: Container(
                    color: Colors.white,
                    child: Container(
                      padding: const EdgeInsets.all(5),
                      decoration: BoxDecoration(
                          border: accountErrStatus
                              ? const Border.fromBorderSide(
                                  BorderSide(color: Colors.red))
                              : null,
                          color: const Color.fromRGBO(249, 249, 250, 1),
                          borderRadius:
                              const BorderRadius.all(Radius.circular(10))),
                      child: CupertinoTextField(
                        maxLength: 30,
                        placeholder: "账号",
                        focusNode: accountFocusNode,
                        controller: accountTextEditingController,
                        decoration: const BoxDecoration(border: null),
                        clearButtonMode: OverlayVisibilityMode.editing,
                      ),
                    ),
                  ),
                ),
                const SizedBox(
                  width: double.infinity,
                  height: 20,
                ),
                CupertinoApp(
                  debugShowCheckedModeBanner: false,
                  home: Container(
                    color: Colors.white,
                    child: Container(
                      padding: const EdgeInsets.all(5),
                      decoration: BoxDecoration(
                          border: passwordErrStatus
                              ? const Border.fromBorderSide(
                                  BorderSide(color: Colors.red))
                              : null,
                          color: const Color.fromRGBO(249, 249, 250, 1),
                          borderRadius:
                              const BorderRadius.all(Radius.circular(10))),
                      child: CupertinoTextField(
                        maxLength: 30,
                        obscureText: true,
                        placeholder: "密码",
                        focusNode: passwordFocusNode,
                        controller: passwordTextEditingController,
                        decoration: const BoxDecoration(border: null),
                        clearButtonMode: OverlayVisibilityMode.editing,
                        onChanged: (password) {},
                      ),
                    ),
                  ),
                ),
                const SizedBox(
                  width: double.infinity,
                  height: 20,
                ),
                CupertinoApp(
                  debugShowCheckedModeBanner: false,
                  home: Container(
                    color: Colors.white,
                    child: Container(
                      padding: const EdgeInsets.all(5),
                      decoration: BoxDecoration(
                          border: passwordErrStatus
                              ? const Border.fromBorderSide(
                                  BorderSide(color: Colors.red))
                              : null,
                          color: const Color.fromRGBO(249, 249, 250, 1),
                          borderRadius:
                              const BorderRadius.all(Radius.circular(10))),
                      child: CupertinoTextField(
                        maxLength: 30,
                        obscureText: true,
                        placeholder: "重复密码",
                        focusNode: verifyPasswordFocusNode,
                        controller: verifyPasswordTextEditingController,
                        decoration: const BoxDecoration(border: null),
                        clearButtonMode: OverlayVisibilityMode.editing,
                        onChanged: (password) {},
                      ),
                    ),
                  ),
                ),
                const SizedBox(
                  width: double.infinity,
                  height: 20,
                ),
                Container(
                  alignment: Alignment.centerRight,
                  child: Text(
                    warningMsg,
                    style: const TextStyle(fontSize: 12, color: Colors.red),
                  ),
                ),
                const SizedBox(
                  width: double.infinity,
                  height: 20,
                ),
                ElevatedButton(
                    onPressed: () {
                      String account = accountTextEditingController.text;
                      String password = passwordTextEditingController.text;
                      String verifyPassword =
                          verifyPasswordTextEditingController.text;
                      logger.i("account: $account, password: $password");
                      if (StringUtils.isEmpty(account)) {
                        logger.i("账号不允许为空");
                        setWarningMsg("账号不允许为空", true, false, false);
                        FocusScope.of(context).requestFocus(accountFocusNode);
                        return;
                      }
                      if (StringUtils.isEmpty(password)) {
                        logger.i("密码不允许为空");
                        setWarningMsg("请输入密码", false, true, false);
                        FocusScope.of(context).requestFocus(passwordFocusNode);
                        return;
                      }
                      if (StringUtils.isEmpty(verifyPassword)) {
                        logger.i("确认密码不允许为空");
                        setWarningMsg("请输入密码", false, false, true);
                        FocusScope.of(context)
                            .requestFocus(verifyPasswordFocusNode);
                        return;
                      }
                      if (verifyPassword != password) {
                        logger.i("确认密码与密码不匹配");
                        setWarningMsg("确认密码与密码不匹配", false, false, true);
                        FocusScope.of(context)
                            .requestFocus(verifyPasswordFocusNode);
                        return;
                      }
                      TokenUtils.register(account, password).then((result) {
                        if (result.code == 0) {
                          RouteUtils.route2MobileHomePageAndCleanStack();
                        } else {
                          setWarningMsg(
                              StringUtils.defaultIfEmpty(result.message, ""),
                              false,
                              false,
                              false);
                        }
                      });
                    },
                    child: Container(
                      alignment: Alignment.center,
                      width: double.infinity,
                      height: 50,
                      child: const Text(
                        "注册",
                        style: TextStyle(fontSize: 22, color: Colors.black),
                      ),
                    )),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
